package com.sojson.controller.po;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.sojson.annotation.RestMapping;
import com.sojson.config.exception.TransErrorCode;
import com.sojson.controller.po.param.verify.GoodsParamVerify;
import com.sojson.entity.bo.Select;
import com.sojson.entity.po.Goods;
import com.sojson.entity.vo.GoodsVo;
import com.sojson.entity.vo.OrderVo;
import com.sojson.enums.core.EGeneralStatus;
import com.sojson.result.Result;
import com.sojson.service.GoodsService;
import com.sojson.service.OrderService;
import com.sojson.util.token.TokenUtil;

import io.swagger.annotations.Api;

/**
 * 商品表控制器
 * 
 * @author liu
 * @date 2021-03-16
 */
@Api(tags = "商品表管理")
@RestMapping("/goods")
public class GoodsController {

    @Autowired
    private OrderService orderService;
    @Resource
    private GoodsService goodsServiceImpl;

    /**
     * 单条插入
     * 
     * @param goods
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @PreAuthorize("@ss.hasPermi('/goods/insert')")
    public Object insert(@RequestBody Goods goods) throws SQLException, IOException {
        // 校验参数
        Result<?> paramResult = GoodsParamVerify.insert(goods);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        goods.insertInit();
        goodsServiceImpl.insert(goods);
        return Result.success();
    }

    /**
     * 单条插入
     * 
     * @param goods
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @RequestMapping(value = "/insertOne", method = RequestMethod.POST)
    @PreAuthorize("@ss.hasPermi('/goods/insert')")
    public Object insertOne(@RequestBody GoodsVo goods) throws SQLException, IOException {
        // 校验参数
        Result<?> paramResult = GoodsParamVerify.insert(goods);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        goods.insertInit();
        goodsServiceImpl.insertOne(goods);
        return Result.success();
    }

    /**
     * 批量插入
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/insertList", method = RequestMethod.POST)
    public Object insertList(@RequestBody Goods goods) throws SQLException {
        return Result.success(goodsServiceImpl.insertList(goods));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/deleteByDto", method = RequestMethod.POST)
    public Object deleteByDto(@RequestBody Goods goods) throws SQLException {
        return Result.success(goodsServiceImpl.deleteByDto(goods));
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/countAll", method = RequestMethod.GET)
    public Object countAll() throws SQLException {
        int countAll = goodsServiceImpl.countAll();
        return Result.success(countAll);
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public Object findAll() throws SQLException {
        List<Goods> findAll = goodsServiceImpl.findAll();
        return Result.success(findAll);
    }

    /**
     * 根据对象统计数量
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/countByDto", method = RequestMethod.POST)
    public Object countByDto(@RequestBody Goods goods) throws SQLException {
        int countByDto = goodsServiceImpl.countByDto(goods);
        return Result.success(countByDto);
    }

    /**
     * 根据对象查询
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/findByDto", method = RequestMethod.GET)
    @PreAuthorize("@ss.hasPermi('/goods/findByDto')")
    public Object findByDto(Goods goods) throws SQLException {
        List<Goods> findByDto = goodsServiceImpl.findByDto(goods);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @DeleteMapping(value = "/deleteById/{id}")
    @PreAuthorize("@ss.hasPermi('/goods/deleteById/{id}')")
    public Object deleteById(@PathVariable Integer id) throws SQLException {
        goodsServiceImpl.deleteById(id);
        return Result.success();
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/deleteByIdList", method = RequestMethod.POST)
    public Object deleteByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        return Result.success(goodsServiceImpl.deleteByIdList(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public Object updateById(@RequestBody Goods goods) throws SQLException {
        goodsServiceImpl.updateById(goods);
        return Result.success();
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateStaticByIdList", method = RequestMethod.POST)
    public Object updateStaticByIdList(@RequestBody Goods goods) throws SQLException {
        return Result.success(goodsServiceImpl.updateStaticByIdList(goods));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateChangeByIdList", method = RequestMethod.POST)
    public Object updateChangeByIdList(@RequestBody Goods goods) throws SQLException {
        return Result.success(goodsServiceImpl.updateChangeByIdList(goods));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/updatePartialById", method = RequestMethod.POST)
    @PreAuthorize("@ss.hasPermi('/goods/updatePartialById')")
    public Object updatePartialById(@RequestBody Goods goods) throws SQLException {
        // 校验参数
        Result<?> paramResult = GoodsParamVerify.updatePartialById(goods);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 参数初始化
        goods.updateInit();
        goods.setUpdateNull();
        goodsServiceImpl.updatePartialById(goods);
        return Result.success();
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateStaticPartialByIdList", method = RequestMethod.POST)
    public Object updateStaticPartialByIdList(@RequestBody Goods goods) throws SQLException {
        return Result.success(goodsServiceImpl.updateStaticPartialByIdList(goods));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateChangePartialByIdList", method = RequestMethod.POST)
    public Object updateChangePartialByIdList(@RequestBody Goods goods) throws SQLException {
        return Result.success(goodsServiceImpl.updateChangePartialByIdList(goods));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/findById/{id}")
    public Object findById(@PathVariable Integer id) throws SQLException {
        Goods findById = goodsServiceImpl.findById(id);
        return Result.success(findById);
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findByIdList", method = RequestMethod.GET)
    public Object findByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        ArrayList<Goods> findByIdList = goodsServiceImpl.findByIdList(ids);
        return Result.success(findByIdList);
    }

    /**
     * 通过对象查询id列表
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findIdByDto", method = RequestMethod.POST)
    public Object findIdByDto(@RequestBody Goods goods) throws SQLException {
        ArrayList<Integer> findIdByDto = goodsServiceImpl.findIdByDto(goods);
        return Result.success(findIdByDto);
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/clear", method = RequestMethod.POST)
    public Object clear() throws SQLException {
        goodsServiceImpl.clear();
        return Result.success();
    }

    /**
     * 根据对象查询
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @PreAuthorize("@ss.hasPermi('/goods/list')")
    public Object list(GoodsVo goods) throws SQLException {
        List<GoodsVo> list = goodsServiceImpl.list(goods);
        return Result.success(list, list);
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/getInfo/{id}")
    public Object getInfo(@PathVariable Integer id) throws SQLException {
        Goods getInfo = goodsServiceImpl.getInfo(id);
        return Result.success(getInfo);
    }

    /**
     * 根据对象查询
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    @GetMapping("/findSelect")
    public Object findSelect(Goods goods) throws SQLException {
        List<Select> findSelect = goodsServiceImpl.findSelect(goods);
        for (Select select : findSelect) {
            select.init();
        }
        return Result.success(findSelect, findSelect);
    }

    /**
     * 根据对象查询
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    @GetMapping("/selectApp")
    public Object selectApp(Goods goods) throws SQLException {
        goods.setStatus(EGeneralStatus.NORMAL.getCode());
        List<Select> findSelect = goodsServiceImpl.findSelect(goods);
        for (Select select : findSelect) {
            select.init();
        }
        return Result.success(findSelect, findSelect);
    }

    /**
     * 根据对象查询
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/findAppByDto", method = RequestMethod.GET)
    public Object findAppByDto(Goods goods) throws SQLException {
        goods.setStatus(EGeneralStatus.NORMAL.getCode());
        List<Goods> findByDto = goodsServiceImpl.findByDto(goods);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 根据对象查询
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/listApp", method = RequestMethod.GET)
    public Object listApp(GoodsVo goods) throws SQLException {
        goods.setStatus(EGeneralStatus.NORMAL.getCode());
        List<GoodsVo> list = goodsServiceImpl.list(goods);
        return Result.success(list, list);
    }

    /**
     * 根据对象查询
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/listMerchant", method = RequestMethod.GET)
    public Object listMerchant(GoodsVo goods) throws SQLException {
        List<GoodsVo> list = goodsServiceImpl.list(goods);
        return Result.success(list, list);
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @PreAuthorize("@ss.hasPermi('/goods/updateStatus')")
    public Object updateStatus(@RequestBody Goods goods) throws SQLException {
        // 校验参数
        Result<?> paramResult = GoodsParamVerify.updatePartialById(goods);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 参数初始化
        Goods g = new Goods();
        g.updateInit();
        g.setId(goods.getId());
        g.setStatus(goods.getStatus());
        goodsServiceImpl.updateStatus(g);
        return Result.success();
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    @PutMapping("/updateStatusList")
    @PreAuthorize("@ss.hasPermi('/goods/updateStatus')")
    public Object updateStatusList(@RequestBody Goods goods) throws SQLException {
        // 校验参数
        Result<?> paramResult = GoodsParamVerify.updatePartialByIds(goods);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 参数初始化
        Goods g = new Goods();
        g.updateInit();
        g.setIds(goods.getIds());
        g.setStatus(goods.getStatus());
        return Result.success(goodsServiceImpl.updateStatusList(g));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param goods
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @RequestMapping(value = "/updateStatusApp", method = RequestMethod.POST)
    @PreAuthorize("@ss.hasPermi('/goods/updateStatusApp')")
    public Object updateStatusApp(@RequestBody Goods goods) throws SQLException, IOException {
        // 校验参数
        Result<?> paramResult = GoodsParamVerify.updatePartialById(goods);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 参数初始化
        Goods g = new Goods();
        g.updateInit();
        g.setId(goods.getId());
        g.setStatus(goods.getStatus());
        goodsServiceImpl.updateStatusApp(g);
        return Result.success();
    }

    /**
     * 商家首页统计
     * 
     * @return
     */
    @GetMapping("/countApp")
    @PreAuthorize("@ss.login()")
    public Object countAllTypeApp() throws Exception {
        String userNo = TokenUtil.getUserNo();
        GoodsVo goods = new GoodsVo();
        goods.setUserNo(userNo);
        OrderVo order = new OrderVo();
        order.setMerchantNo(userNo);

        // normal 售卖中
        // soldOut 已下架
        // sendOutNo 待发货
        // sendOutYes 待收货
        // retuYes 已完成
        Map<String, Integer> count = new HashMap<>(5);
        count.putAll(goodsServiceImpl.count(goods));
        count.putAll(orderService.count(order));

        return Result.success(count);
    }

}